package com.s7.widget.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administor onAgo 2018\4\16 0016.
 * ListView、GridView 等的适配器
 */
public abstract class BaseListAdapter<T> extends BaseAdapter {

    protected List<T> datas;

    public BaseListAdapter() {
         if (this.datas == null) {
             this.datas = new ArrayList<>();
         }
    }

    public BaseListAdapter(List<T> datas) {
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas == null ? null : datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        BaseViewHolder baseViewHolder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(viewGroup.getContext()).inflate(bindLayoutId(), null);
            baseViewHolder = new BaseViewHolder(convertView);
            convertView.setTag(baseViewHolder);
        }
        baseViewHolder = (BaseViewHolder) convertView.getTag();

        T data;
        if(datas == null || datas.size() <= position){
            data = null;
        } else {
            data = datas.get(position);
        }

        initView(position, data, baseViewHolder);

        return convertView;
    }

    /**
     * 刷新数据（第一次加载数据）
     *
     * @param datas
     */
    public void onRefresh(List<T> datas) {
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param datas
     */
    public void addData(List<T> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    /**
     * 替换指定位置的内容
     *
     * @param position
     * @param data
     */
    public void onReplace(int position, T data) {
        this.datas.set(position, data);
        notifyDataSetChanged();
    }

    /**
     * 删除指定位置的内容
     * 当集合为空时显示空白页
     *
     * @param position
     */
    public void onRemove(int position) {
        this.datas.remove(position);
        notifyDataSetChanged();
    }


    public List<T> getDatas() {
        return datas;
    }

    /**
     * 绑定相对应的item界面
     *
     */
    protected abstract int bindLayoutId();

    /**
     * 将ViewHolder的控件跟内容绑定在一起
     * @param viewHolder
     */
    protected abstract void initView(int position, T data, BaseViewHolder viewHolder);

    public class BaseViewHolder {
        private Map<Integer, View> mViewMap;
        private View itemView;
        public BaseViewHolder(View itemView) {
            this.itemView = itemView;
            mViewMap = new HashMap<>();
        }

        /**
         * 获取设置的view
         * @param id
         * @return
         */
        public <T extends View> T getView(int id) {
            View view = mViewMap.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                mViewMap.put(id, view);
            }
            return (T) view;
        }
    }
}
